﻿
using System.IO;
using ZDK.Infra.Models;

namespace ZDK.Infra.Services
{
    public class CfgService : ICfgService
    {
        Cfg Cfg = null;
        public Cfg Get() => Cfg;

        public bool Init()
        {
            using (var fs = new FileStream("cfg.json", FileMode.Open))
            {
                using (var sr = new StreamReader(fs))
                {
                    try
                    {
                        Cfg = Newtonsoft.Json.JsonConvert.DeserializeObject<Cfg>(sr.ReadToEnd());
                        return true;
                    }
                    catch (System.Exception)
                    {
                        return false;
                    }
                }
            }
        }

        public bool Save()
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(Cfg);
            File.WriteAllText("cfg.json", json);
            return true;
        }
    }
}
